const mongoose = require('mongoose');

const fieldTypeSchema = new mongoose.Schema({
  type: {
    type: String,
    required: true,
    unique: true,
    trim: true
  },
  name: {
    type: String,
    required: true,
    trim: true
  },
  category: {
    type: String,
    enum: ['input', 'selection', 'display', 'composite'],
    required: true
  },
  description: {
    type: String,
    trim: true
  },
  icon: String,
  isActive: {
    type: Boolean,
    default: true
  },
  isSystem: {
    type: Boolean,
    default: false
  },
  version: {
    type: String,
    default: '1.0.0'
  },
  valueSchema: {
    type: Object,
    required: true
  },
  defaultConfig: {
    value: Object,
    validation: Object,
    display: Object
  },
  renderConfig: {
    component: String,
    props: Object,
    events: [String]
  }
}, {
  timestamps: { createdAt: 'createTime', updatedAt: 'updateTime' }
});

// 索引
fieldTypeSchema.index({ type: 1 }, { unique: true });
fieldTypeSchema.index({ category: 1, isActive: 1 });
fieldTypeSchema.index({ isSystem: 1 });

module.exports = mongoose.model('FieldType', fieldTypeSchema);